<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('文档库列表')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">

    <div class="ui-layout-west">
        <div class="box box-main">
            <div class="box-header">
                <div class="box-title">
                    <i class="fa fa-sitemap"></i> 项目结构树
                </div>
                <div class="box-tools pull-right">
                    <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i class="fa fa-chevron-up"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i class="fa fa-chevron-down"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新部门"><i class="fa fa-refresh"></i></button>
                </div>
            </div>
            <div class="ui-layout-content">
                <div id="tree" class="ztree"></div>
            </div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="container-div ">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                    <form id="formId">
                        <div class="select-list">
                            <ul>
                                <li>
                                    <label>所属项目：</label>
                                    <input type="hidden" id="relateId" name="relateId" />
                                    <input type="text" id="relateName" readonly="readonly" value="根目录"/>
                                </li>
                                <li>
                                    <label>名称：</label>
                                    <input type="text" id="title" name="title"/>
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="queryList()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                    <a class="btn btn-warning btn-rounded btn-sm" onclick="backward()"><i class="fa fa-backward"></i>&nbsp;返回根目录</a>
                                </li>
                            </ul>
                        </div>
                    </form>
                </div>

                <div class="col-sm-12 select-table table-striped">
                    <div class="btn-group-sm" id="toolbar" role="group">
                        <a class="btn btn-success" onclick="addDocument()" shiro:hasPermission="cd:document:add">
                            <i class="fa fa-plus"></i> 添加目录
                        </a>
                        <a class="btn btn-primary" onclick="uploadDoc()" shiro:hasPermission="cd:document:edit">
                            <i class="fa fa-edit"></i> 上传
                        </a>
                    </div>
                    <div id="doc-list">

                    </div>
                </div>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: layout-latest-js" />
    <th:block th:include="include :: ztree-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('document:document:edit')}]];
        var removeFlag = [[${@permission.hasPermi('document:document:remove')}]];
        var prefix = ctx + "cd/document";


        $(function() {
            var panehHidden = false;
            if ($(this).width() < 769) {
                panehHidden = true;
            }
            $('body').layout({ initClosed: panehHidden, west__size: 185, resizeWithWindow: false });
            // 回到顶部绑定
            if ($.fn.toTop !== undefined) {
                var opt = {
                    win:$('.ui-layout-center'),
                    doc:$('.ui-layout-center')
                };
                $('#scroll-up').toTop(opt);
            }
            queryItemTree();
            queryList();
        });

        function backward(){
            $('#relateId').val('');
            $('#relateName').val('根目录');
            $('#title').val('');
            queryList()
        }
        function queryItemTree()
        {
            var url = ctx + "cd/document/itemTreeData";
            var options = {
                url: url,
                expandLevel: 2,
                onClick : zOnClick
            };
            $.tree.init(options);

            function zOnClick(event, treeId, treeNode) {
                console.log(treeNode)
                $('#relateId').val(treeNode.id);
                $('#relateName').val(treeNode.name);
                queryList()
            }
        }

        $('#btnExpand').click(function() {
            $._tree.expandAll(true);
            $(this).hide();
            $('#btnCollapse').show();
        });

        $('#btnCollapse').click(function() {
            $._tree.expandAll(false);
            $(this).hide();
            $('#btnExpand').show();
        });

        $('#btnRefresh').click(function() {
            queryItemTree();
        });

        function addDocument(){
            var relateId = $('#relateId').val();
            if(relateId == null || relateId == ''){
                $.modal.alertWarning("请选择一条项目");
            }else {
                $.modal.open("添加目录", prefix + "/add/"+relateId);
            }

        }
        function uploadDoc(){
            var relateId = $('#relateId').val();
            if(relateId == null || relateId == ''){
                $.modal.alertWarning("请选择一条项目");
            }else {
                $.modal.open("上传文件", prefix + "/upload/"+relateId);
            }

        }
        function queryList(){
            var relateId = $('#relateId').val();
            var title = $("#title").val();
            console.log(relateId)
            var url = prefix + "/list";
            const formData = new FormData();
            if(relateId != null && relateId != ''){
                formData.append("relateId", relateId);
            }
            if(title != null && title != ''){
                formData.append("title", title);
            }
            fetch(url, {
                method: "POST",
                body: formData
            }).then(response => response.json()).then(data => {
                    console.log(data)
                if(data.code == 0){
                    var arr = data.data;
                    var template = `<table style="margin-top: 10px;width: 100%">`
                    template += `<thead><tr><th style="width: 60%;height: 32px">名称</th><th style="width: 20%;height: 32px">创建时间</th><th style="width: 20%;height: 32px">类型</th></tr></thead>`;
                    template += `<tbody>`
                    arr.forEach(function (item){
                        if(item.type == '1'){
                            template+= `<tr  class='fa-hover'><td style="width: 60%;height: 32px"><span class="glyphicon glyphicon-folder-open" aria-hidden="true" style="padding:0px 15px;"></span>`+item.title+`</td><td style="width: 20%;height: 32px">`+item.createTime+`</td><td style="width: 20%;height: 32px">文件夹</td></tr>`
                        }
                    })
                    template += `</tbody></table>`;

                    $("#doc-list").html(template);

                }
            });
        }
    </script>
    <style >
        .fa-hover{width:100%;cursor:pointer;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:#222222;line-height:32px;height:32px;padding-left:10px;border-radius:4px;}
        .fa-hover:hover{background-color:#1d9d74;color:#fff;text-decoration:none}
        .ui-layout-west {
            width: 254px !important;
        }
        .ui-layout-resizer-west-open{
            left: 254px !important;
        }
        .ui-layout-resizer-west-close{
            left: 0px !important;
        }
    </style>
</body>
</html>
